<template>
  <div class="procurementstrategy">
    <pb-card height="100%" width="100%" padding="16px">
      <div class="top">
        菜谱计划生成采购计算方式
      </div>
      <div class="weighted">
        <span class="mr-1">计重商品：</span>
        <pb-radio-group v-model="strategyForm.weightedGoods" :optionList='weightedGoodslist'>
        </pb-radio-group>
      </div>
      <div class="weighted mt-2">
        <span class="mr-1">计数商品：</span>
        <pb-radio-group v-model="strategyForm.countingProducts" :optionList='countingProductslist'>
        </pb-radio-group>
      </div>
      <div class="weighted mt-3">
        <span class="mr-2">菜谱计划生成采购方式保留</span>
        <pb-input type="text" :width="'80px'" class="mr-1"
          v-model.number.trim="strategyForm.mealGeneratePurchaseDecimal" @blur="DecimalBlur" />
        <span class="ml-1">小数</span>
        <pb-icon name="info" color="rgba(0,0,0,0.35)" class="ml-3 mt-0.5"></pb-icon>
        <span :style="{ 'fontSize': '14px' }" class="ml-1">数字范围1-9</span>
      </div>
      <div class="weighted mt-4">
        <span class="mr-1">同商品不同规格策略：</span>
        <pb-radio-group v-model="strategyForm.CationStrategy" :optionList='cationStrategylist'>
        </pb-radio-group>
      </div>
      <div class="weighted mt-4">
        <span>周采购审核截止时间：</span> <pb-select v-model="strategyForm.weekPpurchaseTime" placeholder="请选择星期" keys="timevalue"
          label="timename" :options="weekTimeList" width="160" />
        <el-time-picker v-model="Timevalue" placeholder="请选择时间" class="m-l3" format="HH:mm" value-format="HH:mm"
          style="width: 160px;height: 35px;" size="default" />
      </div>
      <div class="weighted mt-4">
        <span>周采购（增补）-荤 审核截止时间：</span>
        <el-time-picker v-model="tmpTime" placeholder="请选择时间" class="m-l3" format="HH:mm" value-format="HH:mm"
          style="width: 160px;height: 35px;" size="default" />
      </div>
      <div class="weighted mt-4">
        <span>周采购（增补）-素 审核截止时间：</span>
        <el-time-picker v-model="tmpVegetablesTime" placeholder="请选择时间" class="m-l3" format="HH:mm" value-format="HH:mm"
          style="width: 160px;height: 35px;" size="default" />
      </div>
      <!-- v-hasPerm="'center:procurementstrategy:save'" -->
      <div class="mt-5" v-hasPerm="'center:procurementstrategy:save'">
        <pb-button size="small" @click="editPurchase">保存</pb-button>
      </div>
    </pb-card>
  </div>
</template>
<script setup lang="ts">
import { GetPurchaseParamListApi, EditPurchaseParamListApi } from '@/api/Purchasing'
import { createMessage } from "@/Public-UIlibrary/pb-message/method";
const Timevalue = ref('')
const tmpTime = ref('')
const tmpVegetablesTime = ref('')
const weekTimeList = [{
  timevalue: 1,
  timename: '周一'
}, {
  timevalue: 2,
  timename: '周二'
}, {
  timevalue: 3,
  timename: '周三'
}, {
  timevalue: 4,
  timename: '周四'
}, {
  timevalue: 5,
  timename: '周五'
}, {
  timevalue: 6,
  timename: '周六'
}, {
  timevalue: 7,
  timename: '周日'
}]
const strategyForm = reactive({
  weightedGoods: '0',
  weightedGoodsID: '',
  countingProducts: '0',
  countingProductsID: '',
  mealGeneratePurchaseDecimal: '2',
  mealGeneratePurchaseDecimalID: '',
  CationStrategy: '1',
  CationStrategyID: '',
  weekPpurchaseTime: '',
  weekPpurchaseId: '',
  tmpPurchaseVerifyId: '',
  tmpPurchaseVerifyScheduledTimeVegetablesid: ''
} as any)
const weightedGoodslist = [{
  value: '0',
  lable: '四舍五入'
},
{
  value: '1',
  lable: '按实际计重'
}
]
const countingProductslist = [{
  value: '0',
  lable: '向上取整'
},
{
  value: '1',
  lable: '向下取整'
}
]
const cationStrategylist = [{
  value: '0',
  lable: '单价高优先'
},
{
  value: '1',
  lable: '单价低优先'
},
{
  value: '2',
  lable: '平均价优先'
}
]
const DecimalBlur = (el: any) => {
  // console.log(el, '222')
  if (Number(el) < 1) {
    strategyForm.mealGeneratePurchaseDecimal = 1
    // console.log('2221321321', strategyForm.mealGeneratePurchaseDecima)
  } else if (Number(el) > 9) {
    strategyForm.mealGeneratePurchaseDecimal = 9
  }
}
const GetPamrsListfn = () => {
  GetPurchaseParamListApi({}).then(res => {
    let list = res.data
    list.forEach((el: any, index: number) => {
      if (el.paramCode == 'purchaseCalcWayWeight') {
        strategyForm.weightedGoods = el.paramValue
        strategyForm.weightedGoodsID = el.paramSettingId
      } else if (el.paramCode == 'purchaseCalcWayCount') {
        strategyForm.countingProducts = el.paramValue
        strategyForm.countingProductsID = el.paramSettingId
      } else if (el.paramCode == 'mealGeneratePurchaseDecimal') {
        strategyForm.mealGeneratePurchaseDecimal = el.paramValue
        strategyForm.mealGeneratePurchaseDecimalID = el.paramSettingId
      } else if (el.paramCode == 'purchaseStrategy') {
        strategyForm.CationStrategy = el.paramValue
        strategyForm.CationStrategyID = el.paramSettingId
      } else if (el.paramCode == 'weekPurchaseVerifyScheduledTime') {
        strategyForm.weekPpurchaseTime = JSON.parse(el.paramValue).week;
        strategyForm.weekPpurchaseId = el.paramSettingId
        Timevalue.value = JSON.parse(el.paramValue).time
      } else if (el.paramCode == 'tmpPurchaseVerifyScheduledTime') {
        strategyForm.tmpPurchaseVerifyId = el.paramSettingId
        tmpTime.value = JSON.parse(el.paramValue).time
      } else if (el.paramCode == 'tmpPurchaseVerifyScheduledTimeVegetables') {
        strategyForm.tmpPurchaseVerifyScheduledTimeVegetablesid = el.paramSettingId
        tmpVegetablesTime.value = JSON.parse(el.paramValue).time
      }
    })
  })
}
const editPurchase = () => {
  let { weightedGoods, weightedGoodsID, countingProducts, countingProductsID, mealGeneratePurchaseDecimal, mealGeneratePurchaseDecimalID, CationStrategy, CationStrategyID } = strategyForm
  EditPurchaseParamListApi({
    "paramSettingEditReqs": [
      {
        "paramSettingId": weightedGoodsID,
        "paramValue": weightedGoods
      },
      {
        "paramSettingId": countingProductsID,
        "paramValue": countingProducts
      },
      {
        "paramSettingId": mealGeneratePurchaseDecimalID,
        "paramValue": mealGeneratePurchaseDecimal
      },
      {
        "paramSettingId": CationStrategyID,
        "paramValue": CationStrategy
      },
      {
        "paramSettingId": strategyForm.weekPpurchaseId,
        "paramValue": JSON.stringify({
          week: strategyForm.weekPpurchaseTime,
          time: Timevalue.value
        })
      },
      {
        "paramSettingId": strategyForm.tmpPurchaseVerifyId,
        "paramValue": JSON.stringify({
          time: tmpTime.value
        })
      },
      {
        "paramSettingId": strategyForm.tmpPurchaseVerifyScheduledTimeVegetablesid,
        "paramValue": JSON.stringify({
          time: tmpVegetablesTime.value
        })
      }
    ]
  }).then(res => {
    if (res.code == 2000) {
      createMessage({
        message: '保存成功'
      })
      GetPamrsListfn()
    }
  })
}
onMounted(() => {
  GetPamrsListfn()
})
</script>
<style lang="scss" scoped>
.procurementstrategy {
  height: 100%;

  .top {
    font-family: PingFang SC, PingFang SC;
    font-weight: 600;
    font-size: 16px;
    color: rgba(0, 0, 0, 0.85);
    margin-bottom: 20px;
  }

  .weighted {
    display: flex;
    align-items: center;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    font-size: 16px;
    color: rgba(0, 0, 0, 0.65);
  }
}
</style>
